﻿using System;
using System.Windows.Forms;
using Saturn.App.Common;
using System.Data;
using Saturn.App.FormOper;
using Saturn.AppSQLite;

namespace Saturn.App
{
    static class Program
    {

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [MTAThread]
        static void Main()
        {
            //DbCheck.CheckDbExit();
            try
            {
                var sop = new SaturnWebReference.SaturnWebService();
                try
                {
                    sop.HelloWorld();
                    //DataTable dtUser = sop.GetUserData("");
                    //DataInterface.GetSysUsers(dtUser);
                }
                catch (Exception ex)
                {

                }
            }
            catch (Exception ex)
            {

            }
            try
            {

                var handler = new ThreadExceptionHandler();
                AppDomain.CurrentDomain.UnhandledException += handler.CurrentDomain_UnhandledException;

                var login = new FormLogin();
                login.CallBack += res => Application.Run(new FormMain());

                //开启扫描
                //GetScanData.OpenScan();

                login.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

                //GetScanData.RemoveScan();

                MessageBox.Show("内存占用过大应用将关闭，稍后请重新打开");

                string str = "";
                string strDateInfo = "出现应用程序未处理的异常：" + DateTime.Now.ToString() + "\r\n";

                if (ex != null)
                {
                    str = string.Format(strDateInfo + "异常类型：{0}\r\n异常消息：{1}\r\n异常信息：{2}\r\n",
                    ex.GetType().Name, ex.Message, ex.StackTrace);
                }

                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
        }

        //**************
        internal class ThreadExceptionHandler
        {
            /// <summary>
            /// 捕获非ui线程的异常
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            public void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                DialogResult result = DialogResult.Cancel;
                try
                {
                    Exception ex = (Exception)e.ExceptionObject;
                    result = ShowThreadExceptionDialog("应用程序错误", ex);
                }
                catch (Exception exc)
                {
                    try
                    {
                        MessageBox.Show("严重的应用程序错误：" + exc.Message, "应用程序错误", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    }
                    finally
                    {
                        Application.Exit();
                        System.Diagnostics.Process.GetCurrentProcess().Kill();
                    }
                }
                // 点中止时退出程序
                if (result == DialogResult.Abort)
                {
                    Application.Exit();
                    System.Diagnostics.Process.GetCurrentProcess().Kill();
                }

            }

            /// <summary>
            /// 创建错误信息并显示
            /// </summary>
            /// <param name="title"></param>
            /// <param name="e"></param>
            /// <returns></returns>
            private DialogResult ShowThreadExceptionDialog(string title, Exception e)
            {
                string errorMsg = "应用程序错误，请联系管理员，" +
                    "错误信息:\n\n";
                errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
                //TODO 在这边记下日志，一般情况下我们可以自定义日志 
                return MessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question,
                                       MessageBoxDefaultButton.Button1);
            }
        }
        //*************
    }


}